package com.aiyun.elasticjest.web;



import javax.validation.Valid;

import com.aiyun.elasticjest.domain.Cat;
import com.aiyun.elasticjest.repository.CatRepository;
import org.slf4j.Logger;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.slf4j.LoggerFactory.getLogger;

@RestController
@RequestMapping("/api")
public class CatResource {

	private final Logger logger = getLogger(CatResource.class);

	private final CatRepository catRepository;

	public CatResource(CatRepository catRepository) {
		this.catRepository = catRepository;
	}


	@GetMapping("/cats")
	public Page<Cat> getAllCat(Pageable pageable) {
		return catRepository.findAll(pageable);
	}

	@PostMapping("/cats")
	public Cat add(@RequestBody @Valid Cat cat) {
		catRepository.save(cat);
		return cat;
	}
}
